package com.alan.dfs.msg;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author shixing
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
@ApiModel("RestPageResponse")
public class RestPageResponse<T> {

    @ApiModelProperty(value = "error code", required = true)
    private long code;
    @ApiModelProperty(value = "error message", required = true)
    private String message;
    @ApiModelProperty(value = "data", required = true)
    PageData<T> data;

    public static <T> RestPageResponse<T> ok() {
        return ok(Code.SUCCESS);
    }

    public static <T> RestPageResponse<T> ok(Code code) {
        return ok(code, 0, null);
    }

    public static <T> RestPageResponse<T> ok(long total, List<T> rows) {
        return ok(Code.SUCCESS, total, rows);
    }

    public static <T> RestPageResponse<T> ok(Code code, long total, List<T> rows) {
        return new RestPageResponse<>(code, total, rows);
    }

    public static <T> RestPageResponse<T> error(Code code) {
        return ok(code);
    }

    public static <T> RestPageResponse<T> error(int errorCode, String message) {
        return new RestPageResponse<>(errorCode, message, 0, null);
    }

    public RestPageResponse() {
        this(Code.SUCCESS);
    }

    public RestPageResponse(Code code) {
        this(code, 0, null);
    }

    public RestPageResponse(long total, List<T> rows) {
        this(Code.SUCCESS, total, rows);
    }

    public RestPageResponse(Code code, long total, List<T> rows) {
        this(code.getCode(), code.getMessage(), total, rows);
    }

    public RestPageResponse(long code, String message, long total, List<T> rows) {
        this.code = code;
        this.message = message;
        this.data = new PageData<>(total, rows);
    }

}
